package com.gujiayue.common.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.google.common.collect.Lists;

/**
 * 全局跨域
 */

@Configuration
public class CorsConfig {
	@Bean
	public CorsFilter corsFilter() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		//允许跨域得域名
		List<String> domainList=Lists.newArrayList();
		domainList.add("http://localhost:4000");
		// 1,允许跨域来源
		corsConfiguration.setAllowedOriginPatterns(domainList);
		// 1,允许跨域来源
		corsConfiguration.setAllowedOrigins(domainList);
		// 2,允许任何请求头
		corsConfiguration.addAllowedHeader("*");
		// 3,允许任何方法
		corsConfiguration.addAllowedMethod("*");
		// 4,允许凭证
		corsConfiguration.setAllowCredentials(true);
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", corsConfiguration);
		return new CorsFilter(source);
	}
}